package top.vains.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.vains.entity.Result;
import top.vains.entity.Role;
import top.vains.entity.User;
import top.vains.entity.customer.Pagination;
import top.vains.server.Server;
import top.vains.service.IUserService;
import top.vains.util.ResultUtils;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vains
 * @since 2020-02-17
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口,用户信息的操作接口")
public class UserController {

    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @GetMapping("/getUsers")
    @ApiOperation(value = "获取用户列表", notes = "默认获取所有用户")
    public Result<Pagination> getUsers(Pagination pagination) {
        userService.getUsers(pagination);
        return ResultUtils.success("成功", pagination);
    }

    @PostMapping("/saveUser")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    public Result saveUser(@RequestBody User user) {
        userService.save(user);
        return ResultUtils.success("添加成功！");
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    public Result updateUser(@RequestBody User user) {
        userService.updateById(user);
        return ResultUtils.success("修改成功！");
    }

    @GetMapping("/getUserRole")
    @ApiOperation(value = "查询用户已拥有的角色", notes = "查询用户已拥有的角色")
    public Result getUserRole(Integer userId) {
        List<Role> haveRole = userService.getUserHaveRole(userId);
        return ResultUtils.success("查询成功！", haveRole);
    }

    @GetMapping("/getSystemInfo")
    @ApiOperation(value = "获取服务器信息", notes = "cpu、内存、jvm的信息")
    public Result<Server> getSystemInfo() {
        Server server = new Server();
        server.copyTo();
        return ResultUtils.success("成功", server);
    }

}
